package com.osfw.activiti.service.running.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.osfw.activiti.entity.history.ActHiProcinst;
import com.osfw.activiti.entity.history.ActHiUserinst;
import com.osfw.activiti.entity.running.ActRuUser;
import com.osfw.activiti.mapper.running.ActRuUserMapper;
import com.osfw.activiti.service.history.IActHiUserinstService;
import com.osfw.activiti.service.running.IActRuUserService;
import com.osfw.framework.utils.bean.BeanMapper;
import com.osfw.system.entity.manage.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 流程与身份关系表 服务层实现
 */
@Service
public class ActRuUserServiceImpl extends ServiceImpl<ActRuUserMapper, ActRuUser> implements IActRuUserService {

    @Autowired
    private IActHiUserinstService hiUserinstService;

    @Override
    @Transactional
    public boolean addProcUserinst(Long procDefId, Long procInstId, List<SysUser> users) {
        // 运行中任务关联的用户信息
        if (users != null && !users.isEmpty()) {
            List<ActRuUser> ruUserList = users.stream().map(x -> {
                ActRuUser ruUser = new ActRuUser()
                        .setProcDefId(procDefId)
                        .setProcInstId(procInstId)
                        .setUserType(x.getParams().get("userType").toString())
                        .setGroupId(x.getDeptId()).setGroupName(x.getDeptName())
                        .setUserId(x.getUserId()).setUserName(x.getUserName());
                return ruUser;
            }).collect(Collectors.toList());
            boolean saveBatch = saveBatch(ruUserList);
            if (saveBatch) {
                List<ActHiUserinst> hiUserinstList = BeanMapper.mapAsList(ruUserList, ActHiUserinst.class);
                saveBatch = hiUserinstService.saveBatch(hiUserinstList);
            }
            return saveBatch;
        }
        return false;
    }

}
